import { showSuccessToast } from 'vant';
import { ref } from 'vue';

import { lunchWithdraw } from '../api';
import { WithdrawActionConfig } from '../models/WithdrawAction';

const useWithdrawAction = () => {
  const form = ref<any>({
    validate: () => Promise.resolve(),
  });
  const config = ref<WithdrawActionConfig>(new WithdrawActionConfig());
  const pageState = ref({
    loading: false,
    showPicker: false,
    showNumberKeyBoard: false,
  });

  const fetchData = async () => {
    try {
      pageState.value.loading = true;
      await form.value.validate();
      const { data } = await lunchWithdraw(config.value);
      showSuccessToast(data.msg || '提现成功');
    } catch (error) {
      //
    } finally {
      pageState.value.loading = false;
    }
  };

  return {
    config,
    pageState,
    fetchData,
    form,
  };
};

export default useWithdrawAction;
